package com.qeegoo.b2bautozityp.adapter;

//┏┓　　　┏┓ 
//┏┛┻━━━┛┻┓ 
//┃　　　　　　　┃ 　 
//┃　　　━　　　┃ 
//┃　┳┛　┗┳　┃ 
//┃　　　　　　　┃ 
//┃　　　┻　　　┃ 
//┃　　　　　　　┃ 
//┗━┓　　　┏━┛ 
//┃　　　┃  神兽保佑　　　　　　　　 
//┃　　　┃  代码无BUG！ 
//┃　　　┗━━━┓ 
//┃　　　　　　　┣┓ 
//┃　　　　　　　┏┛ 
//┗┓┓┏━┳┓┏┛ 
// ┃┫┫　┃┫┫ 
// ┗┻┛　┗┻┛

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kelin.mvvmlight.messenger.Messenger;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.moudle.order.model.OrderChildItem;
import com.qeegoo.b2bautozityp.moudle.order.model.OrderData;
import com.qeegoo.b2bautozityp.moudle.order.model.OrderFooterItem;
import com.qeegoo.b2bautozityp.moudle.order.model.OrderHeaderItem;
import com.qeegoo.b2bautozityp.moudle.order.viewmodel.OrderListViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lk on 2017/9/14.
 */

public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static int ITEM_HEADER = 1, ITEM_CONTENT = 2, ITEM_FOOTER = 3;
    private final int adapterType;

    private List<Object> datas;

    public OrderAdapter(int adapterType) {
        this.adapterType = adapterType;
        datas = new ArrayList<>();
    }

    public void addAllDatas(List<Object> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void addDatas(List<Object> data) {
        if (datas != null) {
            datas.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_HEADER) {
            return new ViewHolderHeader(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_header_order, parent, false));
        } else if (viewType == ITEM_CONTENT) {
            return new ViewHolderChild(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_child_order, parent, false));
        } else if (viewType == ITEM_FOOTER) {
            return new ViewHolderFooter(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_footer_order, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        Object object = datas.get(position);
        if (holder instanceof ViewHolderHeader) {
            OrderHeaderItem headerItem = (OrderHeaderItem) object;
            ((ViewHolderHeader) holder).mOrderId.setText("订单号:" + headerItem.itemContent.code);
            ((ViewHolderHeader) holder).mOrderStatus.setText(headerItem.itemContent.orderStatusText);
            ((ViewHolderHeader) holder).mOrderTime.setText("下单时间:" + headerItem.itemContent.createTime);
            ((ViewHolderHeader) holder).mWrapLayout.setOnClickListener((v) -> {

                mOnItemClickListener.onItemClick(((ViewHolderHeader) holder).mWrapLayout, ((OrderHeaderItem) object).realPosition);
            });

        } else if (holder instanceof ViewHolderChild) {
            OrderChildItem childItem = (OrderChildItem) object;
            ((ViewHolderChild) holder).mGoodsCount.setText("x" + childItem.itemContent.orderingQuantity);
            ((ViewHolderChild) holder).mGoodsInfo.setText(childItem.itemContent.goodsName);
            ((ViewHolderChild) holder).mGoodsPrice.setText("¥ " + childItem.itemContent.unitPrice);
            ((ViewHolderChild) holder).mFlag.setText(childItem.itemContent.productTypeText);
            ((ViewHolderChild) holder).mWrapLayout.setOnClickListener((v) -> {
                mOnItemClickListener.onItemClick(((ViewHolderChild) holder).mWrapLayout, ((OrderChildItem) object).realPosition);
            });

        } else if (holder instanceof ViewHolderFooter) {
            OrderFooterItem footerItem = (OrderFooterItem) object;
            ((ViewHolderFooter) holder).mOrder.setText("订单金额（不包含运费）");
            ((ViewHolderFooter) holder).mOrderPrice.setText("¥ " + footerItem.itemContent.totalMoney);
//            ((ViewHolderFooter) holder).mOprBtn.setText("去发货");
            ((ViewHolderFooter) holder).mWrapLayout.setOnClickListener((v) -> {
                mOnItemClickListener.onItemClick(((ViewHolderFooter) holder).mWrapLayout, ((OrderFooterItem) object).realPosition);
            });

            ((ViewHolderFooter) holder).mOprBtn.setOnClickListener((v) -> {
                Messenger.getDefault().send(new OrderData(adapterType, footerItem.itemContent.code), OrderListViewModel.class.getSimpleName());
            });
            ((ViewHolderFooter) holder).mOprBtn.setVisibility("120".equals(footerItem.itemContent.orderStatus) || "150".equals(footerItem.itemContent.orderStatus) ? View.VISIBLE : View.GONE);
            if (adapterType == Constants.AdapterType_0) {
                ((ViewHolderFooter) holder).mOprBtn.setText( "去发货");
            } else {
                ((ViewHolderFooter) holder).mOprBtn.setText( "确认收货");
            }
        }

    }

    @Override
    public int getItemCount() {
        int count = (datas == null ? 0 : datas.size());
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.get(position) instanceof OrderHeaderItem) {
            return ITEM_HEADER;
        } else if (datas.get(position) instanceof OrderChildItem) {
            return ITEM_CONTENT;
        } else if (datas.get(position) instanceof OrderFooterItem) {
            return ITEM_FOOTER;
        }
        return ITEM_CONTENT;
    }

    public static class ViewHolderChild extends RecyclerView.ViewHolder {

        private TextView mGoodsPrice;
        private TextView mGoodsInfo;
        private TextView mGoodsCount;
        private TextView mFlag;
        private ImageView mGoodsLogo;
        private View mWrapLayout;

        public ViewHolderChild(View view) {
            super(view);
            mGoodsPrice = (TextView) view.findViewById(R.id.tv_goods_price);
            mGoodsInfo = (TextView) view.findViewById(R.id.tv_goods_info);
            mGoodsCount = (TextView) view.findViewById(R.id.tv_goods_count);
            mGoodsLogo = (ImageView) view.findViewById(R.id.iv_goods_logo);
            mFlag = (TextView) view.findViewById(R.id.tv_flag);
            mWrapLayout = view.findViewById(R.id.wrap_layout);
        }
    }

    class ViewHolderHeader extends RecyclerView.ViewHolder {

        private TextView mOrderStatus;
        private TextView mOrderId;
        private TextView mOrderTime;
        private View mWrapLayout;

        public ViewHolderHeader(View view) {
            super(view);
            mOrderStatus = (TextView) view.findViewById(R.id.tv_order_status);
            mOrderId = (TextView) view.findViewById(R.id.tv_order_id);
            mOrderTime = (TextView) view.findViewById(R.id.tv_order_time);
            mWrapLayout = view.findViewById(R.id.wrap_layout);
        }
    }

    class ViewHolderFooter extends RecyclerView.ViewHolder {
        private TextView mOprBtn;
        private TextView mOrder;
        private TextView mOrderPrice;
        private View mWrapLayout;

        public ViewHolderFooter(View view) {
            super(view);
            mOprBtn = (TextView) view.findViewById(R.id.tv_opreate_btn);
            mOrder = (TextView) view.findViewById(R.id.tv_order);
            mOrderPrice = (TextView) view.findViewById(R.id.tv_order_price);
            mWrapLayout = view.findViewById(R.id.wrap_layout);
        }
    }

    private OrderAdapter.OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OrderAdapter.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}
